<?php

namespace App\Http\Controllers\Api\v1;

use App\Http\Requests\Api\v1\CardRequest;
use App\Http\Requests\Api\v1\CardEditRequest;
use App\Http\Resources\CardResource;
use App\Models\Card;
use App\Models\Bank;
use App\Models\Configuration;
use App\Models\User;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;

class CardController extends Controller
{
    public function index(Request $request)
    {
        $user = User::getUser(['tiktokid'=>$request->header('Userid')]);
        $card = Card::query()->where('user_id','=',$user->id)->get();
        return $this->success($card);
    }


    public function list(Request $request)
    {
        $user = User::getUser(['tiktokid'=>$request->header('Userid')]);
        $data['card'] = Card::query()->with('bank')->where('user_id','=',$user->id)
            ->where(function ($query){
                $query->where('bank_id','=',0)->orWhere('bank_id','>',2);
            })
            ->get();
        $data['usdt'] = Card::query()->with('bank')->where('user_id','=',$user->id)->whereIn('bank_id',[1,2])->get();
        return $this->success($data);
    }


    public function store(CardRequest $request, Card $card)
    {
        $user = User::getUser(['tiktokid'=>$request->header('Userid')]);
        /*
        if (!Hash::check($request->password, $user->password)) {
            $this->errorResponse(Response::HTTP_OK, trans('error.10005'), 10005);
        }
        */
        if($request->bank_id == 0){
            if(config('app.operation_country') == 'vn'){
                $this->errorResponse(Response::HTTP_OK, trans('error.422'), 422);
            }

            if(Card::query()->where('bank_id','=',0)->where('user_id','=',$user->id)->first()){
                $this->errorResponse(Response::HTTP_OK, trans('error.10040'), 10040);
            }
        }elseif ($request->bank_id > 2){
            if(Card::query()->where('bank_id','>',2)->where('user_id','=',$user->id)->first()){
                $this->errorResponse(Response::HTTP_OK, trans('error.10040'), 10040);
            }
        }else{
            if(Card::query()->whereIn('bank_id',[1,2])->where('user_id','=',$user->id)->first()){
                $this->errorResponse(Response::HTTP_OK, trans('error.10040'), 10040);
            }
        }

        if(Card::query()->withTrashed()->where('number','=',$request->number)->where('user_id','<>',$user->id)->first()){
            $this->errorResponse(Response::HTTP_OK, trans('error.10049'), 10049);
        }


        $card->fill($request->all());
        $card->user()->associate($user);
        $card->save();
        return $this->success(new CardResource($card));
    }


    /**
     * @throws AuthorizationException
     */
    public function show(Card $card)
    {
        $this->authorize('view', $card);
        return $this->success(new CardResource($card));
    }

    /**
     * @throws AuthorizationException
     */
    public function update(Card $card, CardRequest $request)
    {
        $user = $request->user();

        if (!Hash::check($request->password, $user->password)) {
            $this->errorResponse(Response::HTTP_OK, trans('error.10005'), 10005);
        }

        //return $this->fail(40044, trans('error.40044'));
        $this->authorize('update', $card);

        $verifyData = Cache::get($request->get('verification_key'));
        if (!$verifyData) {
            $this->errorResponse(Response::HTTP_OK, trans('error.10002'), 10002);
        }
        //超级验证码判断
        /*
        if ($request->get('verification_code') != config('user.user_super_code')) {
            if (!hash_equals($verifyData['code'], $request->get('verification_code'))) {
                // 返回401
                $this->errorResponse(Response::HTTP_OK, trans('error.10004'), 10004);
            }
        }
        */
        $attributes = $request->only(['number','username','bank_id']);

        $card->delete();
        $new_card = new Card($attributes);
        $new_card->user()->associate($request->user());
        $new_card->save();
        return $this->success(new CardResource($new_card));

    }


    public function editcards(CardEditRequest $request){
        $user = User::getUser(['tiktokid'=>$request->header('Userid')]);
        /*
        if (!Hash::check($request->password, $user->password)) {
            $this->errorResponse(Response::HTTP_OK, trans('error.10005'), 10005);
        }
        */
        $attributes = $request->only(['number','username','ifsc','bank_id']);

        if($request->bank_id == 0){
            Card::query()->where('user_id','=',$user->id)->where('bank_id','=',0)->delete();
        }elseif ($request->bank_id > 2){
            Card::query()->where('user_id','=',$user->id)->where('bank_id','>',2)->delete();
        }else{
            Card::query()->where('user_id','=',$user->id)->whereIn('bank_id',[1,2])->delete();
        }

        $new_card = new Card($attributes);
        $new_card->user()->associate($user);
        $new_card->save();
        return $this->success(new CardResource($new_card));
    }


    // 银行列表
    public function bank(Request $request)
    {
        return $this->success(Bank::getBank());
    }


}
